package com.xiao.webtest.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.xiao.webtest.entity.UserEntity;
import com.xiao.webtest.service.UserService;

@Controller
@RequestMapping(value="/user")
public class UserController {

	private static Log log = LogFactory.getLog(UserController.class);
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="findById",method = RequestMethod.POST)
	public String findById(
			@RequestParam int id,
			Model model 
			){
		
		UserEntity backUser = userService.getById(id);
		model.addAttribute(backUser);
		return "main";
	}
	
	@RequestMapping(value="findByUsername",method = RequestMethod.POST)
	public String findByUsername(@RequestParam String username,
			@RequestParam String password,
			Model model 
			){
		UserEntity user = new UserEntity();
		user.setUsername(username);
		user.setPassword(password);
		
		UserEntity backUser = userService.getByName(username);
		model.addAttribute(backUser);
		return "main";
	}
	
	@RequestMapping(value="update",method = RequestMethod.POST)
	public String update(
			@RequestParam String password,
			@RequestParam int id,
			Model model 
			){
		UserEntity user = new UserEntity();
		user.setId(id);
		user.setPassword(password);
		userService.update(user);
		return "main";
	}
}
